⚡️ Speed up function get_template_variables
by 43%
#71
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 43% (0.43x) speedup for
get_template_variables
inguardrails/utils/templating_utils.py
⏱️ Runtime :
3.07 milliseconds
→2.14 milliseconds
(best of56
runs)📝 Explanation and details
The optimization replaces the expensive
safe_substitute()
operation with direct regex pattern matching using the existingTemplate.pattern
. Here's why this is significantly faster:Key Optimization: Instead of creating a
defaultdict
and performing a full template substitution to discover variables, the optimized code directly uses the regex pattern thatTemplate
internally uses to find variable placeholders.What Changed:
Template(template).safe_substitute(d)
call (92% of original runtime)Template.pattern.finditer()
set
to handle repeated variablesnamed
(e.g.,$foo
) andbraced
(e.g.,${foo}
) variable patternsWhy It's Faster:
finditer()
to process the template once instead of the multi-step substitution processset
for O(1) duplicate checking instead of relying on dictionary key behaviorPerformance Characteristics:
The optimization maintains identical behavior and correctness while dramatically reducing computational overhead, especially beneficial for templates with many escaped dollar signs or large-scale template processing.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit_tests/utils/test_templating_utils.py::test_get_template_variables
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_testsunit_teststest_guard_log_py_testsintegration_teststest_guard_py_testsunit_testsvalidator__replay_test_0.py::test_guardrails_utils_templating_utils_get_template_variables
To edit these changes
git checkout codeflash/optimize-get_template_variables-mh2paoz2
and push.